package config

import (
	"context"
	"github.com/minio/minio-go/v7"
	"github.com/minio/minio-go/v7/pkg/credentials"
	"log/slog"
)

var MinIoClient *minio.Client

type MinIO struct {
	Endpoint   string `yaml:"endpoint"`
	AccessKey  string `yaml:"access_key"`
	SecretKey  string `yaml:"secret_key"`
	BucketName string `yaml:"bucket_name"`
}

func (m *MinIO) Init() {
	client, err := minio.New(
		m.Endpoint, // MinIO服务地址
		&minio.Options{
			Creds: credentials.NewStaticV4(
				m.AccessKey,
				m.SecretKey,
				""),
			Secure: false}) //设置为true的话需要开启https
	if err != nil {
		slog.Error("MinIO初始化失败", "err", err)
	}
	MinIoClient = client
	if flag, _ := MinIoClient.BucketExists(context.Background(), m.BucketName); flag == false {
		slog.Error("MinIO桶："+m.BucketName, "err", err)
	} else {
		slog.Info("MinIO初始化成功")
	}
}
